import pygame
from pygame.sprite import Sprite


class Bullet(Sprite):
    def __init__(self, ai_setting, screen, ship):
        super(Bullet, self).__init__()
        self.ai_setting = ai_setting
        self.screen = screen
        self.bullet_color = ai_setting.bullet_color
        self.bullet_speed_factor = ai_setting.bullet_speed_factor

        self.rect = pygame.Rect(0, 0, ai_setting.bullet_width, ai_setting.bullet_height)
        # 子弹的位置由飞船中心射出
        self.rect.centerx = ship.rect.centerx
        self.y = float(ship.rect.y - ship.height)  # 跟踪子弹图形在屏幕垂直方向上的位置

    # 跟踪子弹图形在屏幕垂直方向上的位置
    def update(self):
        self.y -= self.bullet_speed_factor
        self.rect.y = self.y

    def drawBullet(self):
        pygame.draw.rect(self.screen, self.bullet_color, self.rect)
